<?php

namespace App\Models;


use App\ModelFilters\Qh_CdkeyGroupFilter;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Qh_CdkeyGroup extends BaseModel
{
    protected $table = 'qh_cdkey_groups';

    protected $guarded = [];

    protected $appends = ['usedCount'];

    public function modelFilter(): ?string
    {
        return $this->provideFilter(Qh_CdkeyGroupFilter::class);
    }

    public function cdkey(): HasMany
    {
        return $this->hasMany(Qh_Cdkey::class, 'groupId');
    }

    public function course(): BelongsTo
    {
        return $this->belongsTo(Qh_Course::class, 'courseId');
    }

    public function getUsedCountAttribute(): int
    {
        return Qh_Cdkey::where('groupId', $this->id)->whereIn('status', [2, 4])->count();
    }
}
